import 'package:demo_app_video/commons/router/routes.dart';
import 'package:demo_app_video/commons/sp.dart';
import 'package:demo_app_video/store/user.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:mobx/mobx.dart';

class SettingsPage extends StatefulWidget {
  const SettingsPage({Key? key}) : super(key: key);

  @override
  State<SettingsPage> createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> {
  bool is_kai = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Color(0xffFFFFFF),
        appBar: AppBar(
          elevation: 0,
          scrolledUnderElevation: 0,
          title: Text(
            '系统设置',
            style: TextStyle(fontSize: 16.sp),
          ),
          centerTitle: true,
          backgroundColor: Color(0xffFFFFFF),
        ),
        body: Observer(
          builder: (_) => Column(
            children: [
              InkResponse(
                highlightColor: Colors.transparent,
                radius: 0.0,
                child: Container(
                  width: 360.w,
                  height: 50.h,
                  padding: EdgeInsets.only(left: 16.w, right: 16.w),
                  decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                              color: Color(0xffECECEC), width: 1.sp))),
                  child: Row(
                    children: [
                      Text(
                        '用户协议',
                        style: TextStyle(
                            color: Color(0xff333333), fontSize: 12.sp),
                      ),
                      Expanded(child: Container())
                    ],
                  ),
                ),
                onTap: () {
                  Routes().push(context, Routes.webview, {
                    'url_text': 'https://hpg.rongqide.cn/article/用户协议.html'
                  });
                },
              ),
              InkResponse(
                highlightColor: Colors.transparent,
                radius: 0.0,
                child: Container(
                  width: 360.w,
                  height: 50.h,
                  padding: EdgeInsets.only(left: 16.w, right: 16.w),
                  decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                              color: Color(0xffECECEC), width: 1.sp))),
                  child: Row(
                    children: [
                      Text(
                        '隐私政策',
                        style: TextStyle(
                            color: Color(0xff333333), fontSize: 12.sp),
                      ),
                      Expanded(child: Container())
                    ],
                  ),
                ),
                onTap: () {
                  Routes().push(context, Routes.webview, {
                    'url_text': 'https://hpg.rongqide.cn/article/隐私政策.html'
                  });
                },
              ),

              InkResponse(
                highlightColor: Colors.transparent,
                radius: 0.0,
                child: Container(
                  width: 360.w,
                  height: 50.h,
                  padding: EdgeInsets.only(left: 16.w, right: 16.w),
                  decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                              color: Color(0xffECECEC), width: 1.sp))),
                  child: Row(
                    children: [
                      Text(
                        '个性化推荐',
                        style: TextStyle(
                            color: Color(0xff333333), fontSize: 12.sp),
                      ),
                      Expanded(child: Container()),
                      Switch(
                          value: $UserStore.gexing != '' ? true : false,
                          onChanged: (value) async {
                            if ($UserStore.gexing == '') {
                              $UserStore.setgeXing('a');
                            } else {
                              $UserStore.setgeXing('');
                            }
                            await Sp().setSp(Sp.gxing, $UserStore.gexing);
                          })
                    ],
                  ),
                ),
                onTap: () {
                  Routes().push(context, Routes.webview, {
                    'url_text': 'https://hpg.rongqide.cn/article/隐私政策.html'
                  });
                },
              ),

              //个性化推荐

              $UserStore.token == ''
                  ? Container()
                  : InkResponse(
                      highlightColor: Colors.transparent,
                      radius: 0.0,
                      child: Container(
                        width: 360.w,
                        height: 50.h,
                        padding: EdgeInsets.only(left: 16.w, right: 16.w),
                        decoration: BoxDecoration(
                            border: Border(
                                bottom: BorderSide(
                                    color: Color(0xffECECEC), width: 1.sp))),
                        child: Row(
                          children: [
                            Text(
                              '注销账号',
                              style: TextStyle(
                                  color: Color(0xff333333), fontSize: 12.sp),
                            ),
                            Expanded(child: Container())
                          ],
                        ),
                      ),
                      onTap: () {
                        showDialog(
                          context: context,
                          builder: (context) {
                            return AlertDialog(
                              title: Text('操作确认'),
                              content: Text('您确定执行注销账号操作?'),
                              actions: <Widget>[
                                InkResponse(
                                  child: Text('取消'),
                                  onTap: () {
                                    Navigator.of(context).pop(); // 关闭确认框
                                  },
                                ),
                                InkResponse(
                                  child: Text('确认'),
                                  onTap: () async {
                                    //退出登录
                                    await logout();
                                    Navigator.of(context).pop(); // 关闭确认框
                                  },
                                )
                              ],
                            );
                          },
                        );
                      },
                    ),
              Expanded(child: Container()),
              $UserStore.token == ''
                  ? Container()
                  : InkWell(
                      child: Container(
                        margin: EdgeInsets.only(bottom: 70.h),
                        width: 228.w,
                        height: 50.h,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            color: Color(0xffFF0000),
                            borderRadius: BorderRadius.circular(12.sp)),
                        child: Text(
                          '退出',
                          style: TextStyle(
                              color: Color(0xffFFFFFF),
                              fontSize: 16.sp,
                              fontWeight: FontWeight.bold),
                        ),
                      ),
                      onTap: () => logout(),
                    ),
              Container(
                child: Text('APP备案编号:津ICP备2024012211号-7A'),
              ),
              Container(
                height: 8.h,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text('查询链接:'),
                  InkWell(
                    child: Text(
                      'https://beian.miit.gov.cn',
                      style: TextStyle(color: Colors.blue),
                    ),
                    onTap: () => toUrl(),
                  )
                ],
              ),
              Container(
                height: 30.h,
              )
            ],
          ),
        ));
  }

  logout() async {
    Sp().setSp(Sp.userKey, '');
    $UserStore.setToken('');
    $UserStore.userinfo = ObservableMap.of({});
    Routes().popNowPage(context);
  }

  toUrl() async {
    if (!await launchUrl(Uri.parse('https://beian.miit.gov.cn'))) {
      throw Exception('Could not launch https://beian.miit.gov.cn');
    }
  }
}
